Skip to content

Support System LambdaExpression invocation targets in LightExpression - #570

Closed
dadhi with Copilot wants to merge 2 commits into
masterfrom
copilot/support-system-lambda-invocation-target
Closed

dadhi with Copilot wants to merge 2 commits into
masterfrom
copilot/support-system-lambda-invocation-target

Conversation

Copilot AI commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

LightExpression invocation targets can now be constants containing System.Linq.Expressions.LambdaExpression values. The compiler compiles and invokes these targets through its existing lambda paths.

  • Changes

    • Infer the invocation return type from a constant System lambda.
    • Convert the target for compilation, reusing the converted lambda when inlining is disabled.
    • Cover zero- and one-argument invocations.
  • Example

    var target = System.Linq.Expressions.Expression.Lambda<Func<int>>(
        System.Linq.Expressions.Expression.Constant(42));
    
    var lightLambda = FastExpressionCompiler.LightExpression.Expression.Lambda<Func<int>>(
        FastExpressionCompiler.LightExpression.Expression.Invoke(
            FastExpressionCompiler.LightExpression.Expression.Constant(target)));
    
    var result = lightLambda.CompileFast()();

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement support for System Lambda as LightExpression invocation target Support System LambdaExpression invocation targets in LightExpression Oct 26, 2026
Copilot AI requested a review from dadhi October 26, 2026 21:03
@dadhi

dadhi commented Nov 4, 2026

Copy link
Copy Markdown
Owner

@copilot the idea is not to inline such system expression, instead just compile fast them, without cost of conversion into LightExpression+conversion... But just in case, please check #558 starting from the Expression.cs changes which also uses/implements this feature. Summarize what and how 558 use this feature for.

Copilot AI commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

@copilot the idea is not to inline such system expression, instead just compile fast them, without cost of conversion into LightExpression+conversion... But just in case, please check https://github....

PR #558’s Expression.cs change infers the invoke return type from an Expression<TDelegate> by finding its Compile() return delegate type. The compiler then recognizes expression-tree-valued invocation receivers, collects the receiver and arguments, and emits receiver.Compile() followed by delegate Invoke(), spilling operands when needed to preserve evaluation order. It avoids converting the outer tree or inlining the runtime expression. Notably, #558 calls System’s Compile(), not FEC’s CompileFast(); its main FEC compilation target is the outer expression. That confirms my prior conversion/inlining approach is not the intended design. The LightExpression compiler currently has no CompileFast(System.Linq.Expressions.LambdaExpression) API, so directly applying the requested nested CompileFast path needs a deliberate bridge to the standard FEC compiler.

@dadhi

dadhi commented Nov 4, 2026

Copy link
Copy Markdown
Owner

I do not see a reason to call Compile instead of CompileFast,
so pausing here for the moment..

@jogibear9988

jogibear9988 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

I agree that we should investigate using CompileFast here: fast-compile the original System lambda using the standard FEC compiler, then invoke the resulting delegate. This matches the requested behavior without converting the tree to LightExpression or inlining it. System's Compile() is not inherently required for correctness here.

The architectural consideration is that the LightExpression package currently has no dependency on the standard FEC assembly. Its own CompileFast overloads accept LightExpression trees, so compiling the original System tree directly needs either an explicit dependency on the standard compiler or a bridge. I would prefer an explicit dependency over reflection or duplicated compiler code, if that package dependency is acceptable.

For compatibility, the implementation should preserve receiver/argument evaluation order and the point at which the inner tree is compiled. CompileFast with System fallback would keep unsupported inner expressions working; strict compilation of the outer tree should not silently become a new restriction on runtime inner trees. We should also avoid automatically caching the compiled delegate: reducing/compiling a custom tree can have observable side effects or throw, so changing when/how often that happens needs a separate decision.

Recommendation: yes, work on this once the dependency approach is agreed. Keep it focused on compiling and invoking the original System tree, add regression tests for argument order, exceptions, captured values and unsupported-inner fallback, and measure inner compilation plus repeated outer invocation against the current Compile() path. There are no new performance measurements behind this recommendation yet.

@dadhi dadhi closed this Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support System Lambda as the LightExpression Invocation target

3 participants